package com.csu.backmanagesys.Controller;

import com.csu.backmanagesys.Domain.Account;
import com.csu.backmanagesys.Domain.Order;
import com.csu.backmanagesys.Service.OrderService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.csu.backmanagesys.Service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping("/Orders")
@Controller
public class OrderController {
    @Autowired
    public OrderService OrderService;
    @GetMapping("")
    public ModelAndView order(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("order",orderService.selectOrder());
        String date = orderService.selectOrder().get(0).getOrderDate().toString();
        System.out.println(date);
        modelAndView.setViewName("order_manager");
        return modelAndView;
    }

    @GetMapping("/singleOrder")
    public ModelAndView selectOrder(ModelAndView modelAndView,String search){
        System.out.println(search);
        modelAndView.addObject("order",OrderService.selectOrderById(search));
        modelAndView.setViewName("order_manager");
        return modelAndView;
    }

    @PutMapping("/updateOrderStatus")
    public ModelAndView updateOrderStatus(String orderId){
        ModelAndView modelAndView = new ModelAndView();
        OrderService.updateOrderStatus("S", Integer.parseInt(orderId));
        modelAndView.addObject("order",OrderService.selectOrder());
        modelAndView.setViewName("order_manager");
        return modelAndView;
    }

    @PutMapping("/updateOrderStatusShipped")
    public ModelAndView updateOrderStatusShipped(String orderId){
        ModelAndView modelAndView = new ModelAndView();
        OrderService.updateOrderStatus("P",Integer.parseInt(orderId));
        modelAndView.addObject("order",OrderService.selectOrder());
        modelAndView.setViewName("order_manager");
        return modelAndView;
    }

    @DeleteMapping("/singleOrder")
    public ModelAndView deleteOrder(String orderId){
        ModelAndView modelAndView = new ModelAndView();
        OrderService.deleteOrder(orderId);
        modelAndView.addObject("order",OrderService.selectOrder());
        modelAndView.setViewName("order_manager");
        return modelAndView;
    }

    @GetMapping("/all")
    public ModelAndView selectOrderAll(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("order",OrderService.selectOrder());
        modelAndView.setViewName("order_manager");
        return modelAndView;
    }

    @GetMapping("/done")
    public ModelAndView selectOrderDone(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("order",OrderService.selectOrderStatus("P"));
        modelAndView.setViewName("order_manager");
        return modelAndView;
    }

    @GetMapping("/undone")
    public ModelAndView selectOrderUnDone(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("order",OrderService.selectOrderStatus("S"));
        modelAndView.setViewName("order_manager");
        return modelAndView;
    }

    @Autowired
    private OrderService orderService;
    @PostMapping("/updateOrder")
    public ModelAndView update_user_information(HttpServletRequest httpRequest){
        ModelAndView modelAndView = new ModelAndView();
        String orderid = httpRequest.getParameter("orderId");
        String totalprice = httpRequest.getParameter("totalPrice");
        if(orderid.length() != 0 && totalprice.length()!= 0){
            Order order = new Order();
            order.setOrderId(Integer.parseInt(httpRequest.getParameter("orderId")));
            order.setUsername(httpRequest.getParameter("userid"));
            order.setOrderDate(Timestamp.valueOf(httpRequest.getParameter("orderDate")));
            order.setShipAddress1(httpRequest.getParameter("shipAddress1"));
            order.setShipAddress2(httpRequest.getParameter("shipAddress2"));
            order.setShipCity(httpRequest.getParameter("shipCity"));
            order.setShipState(httpRequest.getParameter("shipState"));
            order.setShipZip(httpRequest.getParameter("shipZip"));
            order.setShipCountry(httpRequest.getParameter("shipCountry"));
            order.setBillAddress1(httpRequest.getParameter("billAddress1"));
            order.setBillAddress2(httpRequest.getParameter("billAddress2"));
            order.setBillCity(httpRequest.getParameter("billCity"));
            order.setBillState(httpRequest.getParameter("billState"));
            order.setBillZip(httpRequest.getParameter("billZip"));
            order.setBillCountry(httpRequest.getParameter("billCountry"));
            order.setCourier(httpRequest.getParameter("courier"));
            order.setTotalPrice(new BigDecimal(totalprice));
            order.setShipToFirstName(httpRequest.getParameter("shipToFirstName"));
            order.setShipToLastName(httpRequest.getParameter("shipToLastName"));
            order.setBillToFirstName(httpRequest.getParameter("billToFirstName"));
            order.setBillToLastName(httpRequest.getParameter("billToLastName"));
            order.setCreditCard(httpRequest.getParameter("creditCard"));
            order.setCardType(httpRequest.getParameter("cardType"));
            order.setExpiryDate(httpRequest.getParameter("expiryDate"));
            order.setLocale(httpRequest.getParameter("locale"));
            order.setStatus(httpRequest.getParameter("status"));
            System.out.println("/updateOrder order="+order.toString());
            orderService.updateOrder(order);
            orderService.updateOrderStatus(order.getStatus() , order.getOrderId());
        }
        modelAndView.addObject("order",orderService.selectOrder());
        System.out.println(orderService.selectOrder().get(0).toString());
        modelAndView.setViewName("order_manager");
        return modelAndView;
    }

    @GetMapping("/getOrderInf")
    @ResponseBody
    public List<Order> getOrderInf(HttpServletRequest req){
        String orderId = req.getParameter("orderId");
        System.out.println(orderService.selectOrderById(orderId).get(0).getOrderDate());
        return orderService.selectOrderById(orderId);
    }



}
